
package com.windgo.service;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.windgo.bean.bo.WxFeedBackBo;
import com.windgo.bean.MarketCategory;
import com.windgo.bean.MarketFeedback;
import com.windgo.bean.MarketUser;
import com.windgo.bean.common.CommonData;
import com.windgo.bean.vo.MarketCategoryChildrenInfo;
import com.windgo.bean.vo.MarketCategoryInfoVO;
import com.windgo.bean.vo.MarketCategoryL1VO;
import com.windgo.bean.wxVo.*;
import com.windgo.bean.vo.MarketCategoryWithChildrenList;
import com.windgo.mapper.MarketCategoryMapper;
import com.windgo.mapper.MarketFeedbackMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author stone
 * @date 2022/09/08 15:11
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    // 添加二级类目时，没有指定一级类目的话，默认一级类目的id为1012000
    private static final Integer DEFAULT_L1_ID = 1012000;


    //Service → Dao（Mapper）
    @Autowired
    MarketCategoryMapper categoryMapper;


    @Override
    public void modify(MarketCategory marketCategory) {
        int i = categoryMapper.updateByPrimaryKey(marketCategory);
    }

    /**
     * 获取一级类目及其二级类目信息
     *
     * @return com.windgo.bean.common.CommonData<com.windgo.bean.vo.MarketCategoryInfoVO>
     * @author mabai
     * @since 2022/10/12 23:52
     */
    @Override
    // 该层返回limit、page、list、pages、total等数据
    public CommonData<MarketCategoryInfoVO> listCategory() {
        // 创建一个返回值对象
        CommonData<MarketCategoryInfoVO> data = new CommonData<>();
        List<MarketCategoryInfoVO> list = categoryMapper.selectAllParentMarketCategoryInfo();
        // 给list内部的childrenList赋值
        for (MarketCategoryInfoVO marketCategoryInfoVO : list) { // marketCategoryInfoVO表示当前父类类目
            // 获取子类类目信息给当前父类类目的children赋值数据
            // 根据父类类目的父id查询其下的子类类目
            Integer pid = marketCategoryInfoVO.getId();
            // 获取当前父类id的子类类目list
            List<MarketCategoryChildrenInfo> children = categoryMapper.selectChildrenCategoryInfoByPid(pid);
            marketCategoryInfoVO.setChildren(children);
        }
        data.setTotal(list.size());
        data.setLimit(list.size());
        data.setPages(1);
        data.setPage(1);
        data.setList(list);
        return data;
    }

    /**
     * 获取一级类目信息
     *
     * @return com.windgo.bean.common.CommonData<com.windgo.bean.vo.MarketCategoryL1VO>
     * @author mabai
     * @since 2022/10/13 14:03
     */
    @Override
    public CommonData<MarketCategoryL1VO> listCategoryL1() {
        CommonData<MarketCategoryL1VO> data = new CommonData<>();
        List<MarketCategoryL1VO> list = categoryMapper.selectMarketCategoryL1();
        data.setTotal(list.size());
        data.setLimit(list.size());
        data.setPages(1);
        data.setPage(1);
        data.setList(list);
        return data;
    }


    /**
     * wx-类目-根据id分类
     *
     * @param id
     * @return
     */
    @Override
    public WxCategoryVo1 selectCategoryById(Integer id) {
        CurrentCategoryEntity currentCategoryEntity = null;
        List<CurrentSubCategoryEntity> currentSubCategory = null;
        try {
            currentCategoryEntity = categoryMapper.selectCategoryById(id);
            currentSubCategory = categoryMapper.selectCategoryByPid(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        WxCategoryVo1 wxCategoryVo1 = new WxCategoryVo1();
        wxCategoryVo1.setCurrentCategory(currentCategoryEntity);
        wxCategoryVo1.setCurrentSubCategory(currentSubCategory);
        return wxCategoryVo1;
    }

    /**
     * wx-类目-首页
     *
     * @return
     */
    @Override
    public WxCategoryVo2 selectAllCategoty() {
        List<CategoryListEntity> categoryListEntity = null;
        CurrentCategoryEntity currentCategoryEntity = null;
        List<CurrentSubCategoryEntity> currentSubCategoryEntityList = null;
        try {
            categoryListEntity = categoryMapper.selectDefaultIndex();

            // for (CategoryListEntity listEntity : categoryListEntity) {
            //     Integer id = listEntity.getId();
            // }
            //TODO 默认的第一个是写死的，后序再修改。
            Integer integer = 1005000;

            currentCategoryEntity = categoryMapper.selectCategoryById(integer);
            currentSubCategoryEntityList = categoryMapper.selectCategoryByPid(currentCategoryEntity.getId());
        } catch (Exception e) {
            e.printStackTrace();
        }
        WxCategoryVo2 wxCategoryVo2 = new WxCategoryVo2();
        wxCategoryVo2.setCategoryList(categoryListEntity);
        wxCategoryVo2.setCurrentCategory(currentCategoryEntity);
        wxCategoryVo2.setCurrentSubCategory(currentSubCategoryEntityList);
        return wxCategoryVo2;
    }


    /**
     * 新增一条类目信息
     *
     * @param marketCategory
     * @return com.windgo.bean.MarketCategory
     * @author mabai
     * @since 2022/10/13 15:20
     */
    @Override
    public MarketCategory create(MarketCategory marketCategory) {
        long currentTimeMillis = System.currentTimeMillis();
        Timestamp timestamp = new Timestamp(currentTimeMillis);
        marketCategory.setAddTime(timestamp);
        marketCategory.setUpdateTime(timestamp);
        marketCategory.setDeleted(false);
        Integer pid = marketCategory.getPid();
        // 如果新建的二级类目的一级类目pid传值为0，则将杂货的id作为默认一级类目
        if ("L2".equals(marketCategory.getLevel()) && pid == 0) {
            marketCategory.setPid(DEFAULT_L1_ID);
        }
        int row = categoryMapper.insertSelective(marketCategory);
        return categoryMapper.selectByPrimaryKey(marketCategory.getId());
    }

    /**
     * 修改商品类目信息
     *
     * @param marketCategoryInfoVO
     * @return void
     * @author mabai
     * @since 2022/10/14 11:36
     */
    @Override
    public void updateCategoryInfo(MarketCategoryWithChildrenList marketCategoryInfoVO) {
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setId(marketCategoryInfoVO.getId());
        marketCategory.setDesc(marketCategoryInfoVO.getDesc());
        marketCategory.setIconUrl(marketCategoryInfoVO.getIconUrl());
        marketCategory.setKeywords(marketCategoryInfoVO.getKeywords());
        marketCategory.setLevel(marketCategoryInfoVO.getLevel());
        marketCategory.setName(marketCategoryInfoVO.getName());
        marketCategory.setPicUrl(marketCategoryInfoVO.getPicUrl());
        marketCategory.setPid(marketCategoryInfoVO.getPid());
        long currentTimeMillis = System.currentTimeMillis();
        Timestamp timestamp = new Timestamp(currentTimeMillis);
        marketCategory.setUpdateTime(timestamp);
        categoryMapper.updateByPrimaryKeySelective(marketCategory);
    }

    /**
     * 商品类目的逻辑删除
     *
     * @param marketCategoryInfoVO
     * @return void
     * @author mabai
     * @since 2022/10/14 10:47
     */
    @Override
    public void logicDelete(MarketCategoryWithChildrenList marketCategoryInfoVO) {
        MarketCategory marketCategory = new MarketCategory();
        marketCategory.setId(marketCategoryInfoVO.getId());
        marketCategory.setDeleted(true);
        long currentTimeMillis = System.currentTimeMillis();
        Timestamp timestamp = new Timestamp(currentTimeMillis);
        marketCategory.setUpdateTime(timestamp);
        categoryMapper.updateByPrimaryKeySelective(marketCategory);
    }

    /**
     * wx-topic-list
     *
     * @param page
     * @param limit
     * @return
     * @author zlh
     */
    @Override
    public CommonData<WxTopicListVo> getTopicList(Integer page, Integer limit) {

        PageHelper.startPage(page, limit);

        List<WxTopicListVo> listVos = categoryMapper.selectWxTopic();
        PageInfo<WxTopicListVo> pageInfo = new PageInfo<>(listVos);
        return CommonData.data(pageInfo);
    }

    /**
     * wx-topic-detail
     *
     * @param id
     * @return
     */
    @Override
    public WxTopicDetailVo getTopicDetailById(Integer id) {

        TopicEntity topicEntity = null;
        List<GoodsEntity> list = null;
        try {
            topicEntity = categoryMapper.getTopicDetailById(id);
            Integer[] goods = topicEntity.getGoods();
            list = new ArrayList<>();
            for (Integer good : goods) {
                GoodsEntity goodsEntity = categoryMapper.getTopicGoodsListById(good);
                list.add(goodsEntity);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        WxTopicDetailVo wxTopicDetailVo = new WxTopicDetailVo();
        wxTopicDetailVo.setGoods(list);
        wxTopicDetailVo.setTopic(topicEntity);

        return wxTopicDetailVo;
    }

    /**
     * wx-topic-related
     *
     * @param id
     * @return
     */
    @Override
    public CommonData<TopicEntity> getTopicRelated(Integer id) {
        PageHelper.startPage(1, 4);
        Integer sortOrder = categoryMapper.selectSortOrderById(id);
        List<TopicEntity> list = categoryMapper.selectRelatedTopic(sortOrder);
        PageInfo<TopicEntity> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    @Autowired
    MarketFeedbackMapper marketFeedbackMapper;

    @Override
    public int addFeedBack(WxFeedBackBo wxFeedBackBo, MarketUser marketUser) {
        MarketFeedback feedback = new MarketFeedback();
        feedback.setContent(wxFeedBackBo.getContent());
        feedback.setFeedType(wxFeedBackBo.getFeedType());
        feedback.setHasPicture(wxFeedBackBo.isHasPicture());
        feedback.setMobile(wxFeedBackBo.getMobile());
        feedback.setPicUrls(wxFeedBackBo.getPicUrls());
        feedback.setUserId(marketUser.getId());
        feedback.setUsername(marketUser.getUsername());
        feedback.setDeleted(false);
        Date date = new Date();
        // SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // simpleDateFormat.
        feedback.setAddTime(date);


        try {
            marketFeedbackMapper.insertSelective(feedback);
            return 200;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 404;
    }


}

